package com.xltt.watchclient.module;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

import com.xltt.watchclient.LogTool;
import com.xltt.watchclient.module.bean.ActiveDevice;
import com.xltt.watchclient.module.bean.OptResult;
import com.xltt.watchclient.module.service.DeviceService;

import org.greenrobot.eventbus.EventBus;

import androidx.appcompat.app.AppCompatActivity;

import static com.xltt.watchclient.module.service.BleController.ACTIVE_DEVICE;
import static com.xltt.watchclient.module.service.BleController.GET_CONNECTED_DEVICES;
import static com.xltt.watchclient.module.service.BleController.REQUEST_LIST;
import static com.xltt.watchclient.module.service.BleController.START_DEVICE;
import static com.xltt.watchclient.module.service.BleController.STOP_REQUEST_LIST;

public class WatchApiHelper {
    Messenger mMessenger;
    ServiceConnection mServiceConnection;
    private Messenger mMessenger1 = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msgFromServer) {
            String result = msgFromServer.getData().getString("result");
            LogTool.d(result);
            OptResult mOptResult = OptResult.fromJson(result);
            EventBus.getDefault().post(mOptResult);
        }
    });


    public void startScan() {
        Message mMessage = new Message();
        mMessage.replyTo = mMessenger1;
        mMessage.what = REQUEST_LIST;
        try {
            mMessenger.send(mMessage);
        } catch (Exception e) {
            LogTool.d(e.toString());
        }
    }

    public void tryConnect(String address) {
        Message mMessage = new Message();
        mMessage.replyTo = mMessenger1;
        mMessage.what = START_DEVICE;
        mMessage.getData().putString("result", address);
        try {
            mMessenger.send(mMessage);
        } catch (Exception e) {
            LogTool.d(e.toString());
        }

    }

    public void stopScan(){
        Message mMessage = new Message();
        mMessage.replyTo = mMessenger1;
        mMessage.what = STOP_REQUEST_LIST;
        try {
            mMessenger.send(mMessage);
        } catch (Exception e) {
            LogTool.d(e.toString());
        }
    }


    public void checkState() {
        Message mMessage = new Message();
        mMessage.replyTo = mMessenger1;
        mMessage.what = GET_CONNECTED_DEVICES;
        try {
            mMessenger.send(mMessage);
        } catch (Exception e) {
            LogTool.d(e.toString());
        }
    }

    public void activeDevice(String name, String pwd) {
        Message mMessage = new Message();
        mMessage.replyTo = mMessenger1;
        mMessage.what = ACTIVE_DEVICE;
        ActiveDevice mActiveDevice = new ActiveDevice();
        mActiveDevice.name = name;
        mActiveDevice.pwd = pwd;
        mMessage.getData().putString("result", mActiveDevice.toJson());
        try {
            mMessenger.send(mMessage);
        } catch (Exception e) {
            LogTool.d(e.toString());
        }
    }

    public void onCreate(AppCompatActivity activity) {
        Intent myIntent = new Intent(activity, DeviceService.class);
        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mMessenger = new Messenger(service);
                Message mMessage = new Message();
                mMessage.replyTo = mMessenger1;
                mMessage.what = -1;
                try {
                    mMessenger.send(mMessage);
                } catch (Exception e) {
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                mMessenger = null;
            }
        };
        activity.bindService(myIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    public void onDestroy(AppCompatActivity activity) {
        activity.unbindService(mServiceConnection);
    }
}
